/*
 * Array.h
 *
 *  Created on: 2012-10-18
 *      Author: yangentao@gmail.com
 */

#pragma once;

#include <assert.h>

namespace yet{

template<typename T>
class Array {
public:
	T* buffer;
	int length;
public:
	Array() :
			buffer(0), length(0) {

	}
	Array(T val, int len) :
			length(len) {
		buffer = new T[len];
		for (int i = 0; i < len; ++i) {
			buffer[i] = val;
		}
	}
	Array(int len) {
		buffer = new T[len];
		length = len;
		memset(buffer, 0, sizeof(T) * len);
	}

	virtual ~Array() {
		release();
	}
public:
	T operator[](int index) const {
		assert(index < length);
		return buffer[index];
	}
	T& operator[](int index) {
		assert(index < length);
		return buffer[index];
	}
public:
	void release() {
		if (buffer) {
			delete[] buffer;
		}
		buffer = 0;
		length = 0;
	}
	T* discard() {
		T* t = buffer;
		buffer = 0;
		length = 0;
		return t;
	}
};

};
